package com.ljx.shiro.controller;

import com.ljx.shiro.common.RespBean;
import com.ljx.shiro.entity.User;
import com.ljx.shiro.service.IUserService;
import com.ljx.shiro.utils.Token;
import com.ljx.shiro.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


/**
 * @Author: Ljx
 * @Date: 2022/4/25 22:39
 * @role:
 */
@RestController
@RequestMapping("/user")
@Api(value = "用户接口",tags = "UserController")
public class UserController {

    private static final Logger logger= LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    private IUserService iUserService;
    @ApiOperation(value = "用户登录",httpMethod = "GET")
    @GetMapping("/login/{account}/{password}")
    public RespBean userLogin(@PathVariable String account, @PathVariable String password){
//        String token = redisTemplate.boundValueOps(account).get();
//        if (token!=null){
//            return RespBean.ok("登录成功 : "+ token);
//        }
        User user = iUserService.userLogin(account, password);
        System.out.println(user);
        if (user==null){
//            logger.info("账号或密码错误登录失败");
            logger.error("账号或密码错误登录失败");
            System.out.println("账号或密码错误登录失败");
            return RespBean.error("账号或密码错误登录失败");
        }
        TokenUtil tku=new TokenUtil();
        String jwt=tku.creatToken(account, user.getRole());
        System.out.println(jwt);
        Token tk=tku.getTokenData(jwt);
        System.out.println(tk);
        return RespBean.ok("登录成功 : "+ tk);
    }

    @PostMapping("/test")
    public String test(){
        return "1123";
    }
}
